﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

namespace ZGameFramework.UI
{

    public class BubbleCellPosition : BubbelCellBase
    {
        public Vector2 from = Vector3.zero;
        public Vector2 to = Vector3.zero;


        private RectTransform rectTransform;

        private TweenRectPosition _tween;
        public TweenRectPosition tween
        {
            get
            {
                if (null == _tween) { _tween = this.gameObject.GetComponent<TweenRectPosition>(); }
                if (null == _tween)
                {
                    _tween = this.gameObject.AddComponent<TweenRectPosition>();
                    tween.enabled = false;
                    tween.useFixedUpdate = true;
                }


                return _tween as TweenRectPosition;
            }
        }

        private void Awake()
        {
            rectTransform = this.gameObject.GetComponent<RectTransform>();
        }

        private void OnEnable()
        {
            rectTransform.anchoredPosition = from;
        }

        public override void StartFadeIn()
        {
           // RectTransform rectTransform;

            tween.duration = duration;
            tween.from = from;
            tween.to = to;

            if (AnimationOrTween.Direction.Forward == tween.direction)
            {
                tween.ResetToBeginning();
                tween.PlayForward();
            }
            else if (AnimationOrTween.Direction.Reverse == tween.direction)
            {
                transform.localPosition = from;
                tween.PlayForward();
            }
        }

        public override void StartFadeOut()
        {
            tween.duration = duration;
            tween.from = from;
            tween.to = to;

            if (AnimationOrTween.Direction.Forward == tween.direction)
            {
                tween.tweenFactor = 1;
                tween.PlayReverse();
            }
            else if (AnimationOrTween.Direction.Reverse == tween.direction)
            {
                tween.ResetToBeginning();
                tween.PlayReverse();
            }
        }


    }
}